package com.useeinfo.oa.modules.repair.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.logger.dao.ActiveLogDao;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import com.useeinfo.oa.modules.repair.entity.RepairOrderLog;
import com.useeinfo.oa.modules.repair.enums.RepairOrderLogOperationEnum;
import com.useeinfo.oa.modules.repair.enums.RepairOrderStatusEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/12 13:56
 * @Description:
 */
@Repository("repairOrderLogDao")
public class RepairOrderLogDao implements CrudDao<RepairOrderLog> {

    private final static Logger logger = LoggerFactory.getLogger(RepairOrderLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

        Map<String, Object> conditionHash = new LinkedHashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String repairOrderId = queryHash.get("repairOrderId");
        if (StringUtils.isNotBlank(repairOrderId)) {
            conditionHash.put("s.repairOrder.id = ?{paramIndex} ", StringConverters.null2Long(repairOrderId));
        }

        String enterpriseId = queryHash.get("enterpriseId");
        if(StringUtils.isNotBlank(enterpriseId)){
            conditionHash.put("s.enterprise.id = ?{paramIndex} ",StringConverters.null2Long(enterpriseId));
        }
        String repairOrderLogOperationEnum = queryHash.get("repairOrderLogOperationEnum");
        if(StringUtils.isNotBlank(repairOrderLogOperationEnum)){
            conditionHash.put("s.repairOrderLogOperationEnum = ?{paramIndex} ",RepairOrderLogOperationEnum.valueOf(repairOrderLogOperationEnum));
        }

        String repairOrderLogOperation = queryHash.get("repairOrderLogOperation");
        if (StringUtils.isNotBlank(repairOrderLogOperation)) {
            String[] statusArray = repairOrderLogOperation.split(",");
            for (int i = 0; i < statusArray.length; i++) {
                if (i == 0) {
                    String k = statusArray.length == 1 ? "repairOrderLogOperationEnum = ?{paramIndex} " : "(repairOrderLogOperationEnum = ?{paramIndex} ";
                    conditionHash.put(k, RepairOrderLogOperationEnum.valueOf(statusArray[i].trim()));
                } else if (i == statusArray.length - 1) {
                    conditionHash.put("or repairOrderLogOperationEnum = ?{paramIndex}) ", RepairOrderLogOperationEnum.valueOf(statusArray[i].trim()));
                } else {
                    String k = String.format("or repairOrderLogOperationEnum = ?{paramIndex} %" + i + "s", "");
                    conditionHash.put(k, RepairOrderLogOperationEnum.valueOf(statusArray[i].trim()));
                }
            }
        }


        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from RepairOrderLog s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<RepairOrderLog> findList(QueryParam queryParam){
        String sqlInfo = queryParam.joinJPQL("select s from RepairOrderLog s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<RepairOrderLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, RepairOrderLog.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public RepairOrderLog findModel(Long id) {
        return entityManager.find(RepairOrderLog.class, id);
    }

    @Override
    public Integer add(RepairOrderLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("RepairOrderLogDaoImpl添加RepairOrderLog成功！");
        return 1;
    }

    @Override
    public Integer update(RepairOrderLog model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id){
        RepairOrderLog repairOrderLog = entityManager.find(RepairOrderLog.class, id);
        entityManager.remove(repairOrderLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(RepairOrderLog model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
